package com.edu.gcu.manager.service.impl;

import com.beust.jcommander.internal.Maps;
import com.edu.gcu.manager.service.ISystemService;
import com.edu.gcu.system.common.ResponseCode;
import com.edu.gcu.system.common.ServerResponse;
import com.edu.gcu.system.common.TokenCache;
import com.edu.gcu.system.dao.AdminMapper;
import com.edu.gcu.system.pojo.Admin;
import com.edu.gcu.system.util.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author polo
 * @version V1.0
 * @description: 系统service接口实现类
 * @date 2018-02-20-12:22
 */

@Service("iSystemService")
public class ISystemServiceImpl implements ISystemService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public ServerResponse login(String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode
                    .ILLEGAL_ARGUMENT.getDesc());
        }
        int resultCount = adminMapper.checkUsername(username);
        if (resultCount <= 0) {
            return ServerResponse.createByErrorMessage("用户不存在!");
        }

        String passwordMD5 = MD5Util.MD5EncodeUtf8(password);
        Admin admin = adminMapper.checkUser(username, passwordMD5, "");

        if (admin == null) {
            return ServerResponse.createByErrorMessage("密码错误!");
        }
        admin.setPassword(StringUtils.EMPTY);
        admin.setLoginTime(new Date(System.currentTimeMillis()));
        return ServerResponse.createBySuccess(admin);
    }

    @Override
    public ServerResponse forgetCheckUser(String username, String name) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(name)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode
                    .ILLEGAL_ARGUMENT.getDesc());
        }
        Admin admin = adminMapper.checkUser(username, "", name);
        if (admin == null) {
            return ServerResponse.createByErrorMessage("用户不存在!");
        }
        Map<String, Object> map = Maps.newHashMap();
        // 存储到本地缓存
        String tokenUUID = UUID.randomUUID().toString().replace("-", "");
        TokenCache.setKey(TokenCache.TOKEN_PREFIX + admin.getUsername(), tokenUUID);
        // 返回结果集
        map.put("token", tokenUUID);
        map.put("username", admin.getUsername());
        return ServerResponse.createBySuccess(map);
    }

    @Override
    public ServerResponse forgetUpdatePwd(String token, String username, String password) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(token) || StringUtils.isBlank(password)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode
                    .ILLEGAL_ARGUMENT.getDesc());
        }
        int resultCount = adminMapper.checkUsername(username);
        if (resultCount <= 0) {
            return ServerResponse.createByErrorMessage("用户不存在!");
        }
        String tokenUUID = TokenCache.getKey(TokenCache.TOKEN_PREFIX + username);
        if (!StringUtils.equals(token, tokenUUID)) {
            return ServerResponse.createByErrorMessage("token已过期或不存在!");
        }
        resultCount = adminMapper.updateByUsername(username, MD5Util.MD5EncodeUtf8(password));
        if (resultCount > 0) {
            return ServerResponse.createBySuccessMessage("更新成功!");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Override
    public ServerResponse updateById(Admin admin) {
        admin.setPassword(MD5Util.MD5EncodeUtf8(admin.getPassword()));
        int resultCount = adminMapper.updateByPrimaryKeySelective(admin);
        admin = adminMapper.selectByPrimaryKey(admin.getId());
        admin.setPassword(StringUtils.EMPTY);
        if (resultCount > 0) {
            return ServerResponse.createBySuccess("更新成功!", admin);
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }
}
